package com.ms.oschina.viewholder;

import android.os.AsyncTask;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ms.oschina.R;
import com.ms.oschina.base.BaseViewHolder;
import com.ms.oschina.base.SuperBaseAdapter;
import com.ms.oschina.util.UiUtils;

/**
 * Created by Administrator on 2016/4/28.
 */
public abstract class LoadMoreViewHolder extends BaseViewHolder {

	private TextView loadmoreRetry;
	private LinearLayout llLoadMore;
	private TextView loadfinish;

	@Override
	public View initView() {
		View view = UiUtils.inflate(R.layout.loadmore);
		loadfinish = (TextView) view.findViewById(R.id.tv_loadfinish);
		loadmoreRetry = (TextView) view.findViewById(R.id.error_tv_retry);
		llLoadMore = (LinearLayout) view.findViewById(R.id.ll_loadingMore);
		loadmoreRetry.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showLoadMore();
				preLoadMore();
			}
		});
		return view;
	}

	@Override
	public void setData(int position, Object data) {
		// 加载跟多
		if (IsShowLoadMore()) { // 已经是在加载中
			return;
		}
		if (IsShowLoadFinish()) {// 已经是加载全部
			return;
		}
		llLoadMore.setVisibility(View.VISIBLE);
		preLoadMore();
	}

	private boolean IsShowLoadFinish() {
		return loadfinish.getVisibility() == View.VISIBLE;
	}

	private void preLoadMore() {
		LoadMoreTask task = new LoadMoreTask();
		task.execute("执行任务");
	}

	private class LoadMoreTask extends AsyncTask<String, Void, Integer> {
		@Override
		protected Integer doInBackground(String... params) {
			int status = loadMore();
			return status;
		}

		@Override
		protected void onPostExecute(Integer status) {
			// 更新界面
			switch (status) {
			case SuperBaseAdapter.LOADMORE_STATUS_LOADING:
				// 加载中 不做任何操作
				break;
			case SuperBaseAdapter.LOADMORE_STATUS_SUCCESS:
				// 加载成功 关闭对话框
				closeLoadMore();
				break;
			case SuperBaseAdapter.LOADMORE_STATUS_ERROR:
				// 加载失败 显示失败对话框
				showRetry();
				break;
			case SuperBaseAdapter.LOADMORE_STATUS_FINISH:
				// 全部加载
				showFinish();
				break;
			default:
				break;
			}
		}
	}

	public void closeLoadMore() {
		llLoadMore.setVisibility(View.GONE);
		loadmoreRetry.setVisibility(View.GONE);
		loadfinish.setVisibility(View.GONE);
	}

	public void showFinish() {
		llLoadMore.setVisibility(View.GONE);
		loadfinish.setVisibility(View.VISIBLE);
		loadmoreRetry.setVisibility(View.GONE);
	}

	/**
	 * 
	 * @param text
	 *            完成界面的文字
	 */
	public void showFinish(String text) {
		showFinish();
		if (!TextUtils.isEmpty(text))
			loadfinish.setText(text);
	}

	public void showRetry() {
		llLoadMore.setVisibility(View.GONE);
		loadmoreRetry.setVisibility(View.VISIBLE);
		loadfinish.setVisibility(View.GONE);
	}

	public void showLoadMore() {
		llLoadMore.setVisibility(View.VISIBLE);
		loadmoreRetry.setVisibility(View.GONE);
		loadfinish.setVisibility(View.GONE);
	}

	public boolean IsShowLoadMore() {
		if (llLoadMore.getVisibility() == View.VISIBLE
				|| loadmoreRetry.getVisibility() == View.VISIBLE) {
			return true;
		}
		return false;
	}

	/**
	 * 在子线程中更新数据
	 */
	protected abstract int loadMore();
}
